import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sleep/help_style.dart';
import 'package:sleep/provider/fagigue_state_provider.dart';

const colorMap = {
  FatigueState.normal: Colors.green,
  FatigueState.mild: Colors.yellow,
  FatigueState.moderate: Colors.orange,
  FatigueState.severe: Colors.red,
};

class HealthState extends ConsumerWidget {
  const HealthState({super.key});

  @override
  Widget build(BuildContext context, ref) {
    final state = ref.watch(fatigueStateProvider)?.state;

    return Container(
        decoration: HelpStyle.decoration,
        height: 40,
        child: CustomPaint(
          painter: _BackPinter(state ?? FatigueState.normal,
              FatigueState.values.indexOf(state ?? FatigueState.normal)),
          child: Row(
            children: [
              Expanded(
                  child: Text(
                "Normal",
                style: HelpStyle.contextStyle,
                textAlign: TextAlign.center,
              )),
              Expanded(
                  child: Text(
                "Mild",
                style: HelpStyle.contextStyle,
                textAlign: TextAlign.center,
              )),
              Expanded(
                  child: Text(
                "Moderate",
                style: HelpStyle.contextStyle,
                textAlign: TextAlign.center,
              )),
              Expanded(
                  child: Text(
                "Severe",
                style: HelpStyle.contextStyle,
                textAlign: TextAlign.center,
              )),
            ],
          ),
        ));
  }
}

class _BackPinter extends CustomPainter {
  final FatigueState state;
  final int index;

  _BackPinter(this.state, this.index);
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = colorMap[state] ?? Colors.green
      ..style = PaintingStyle.fill;

    final borderRadius = BorderRadius.circular(4.0); // 圆角半径

    double startX = size.width * 0.25 * index;
    final rrect = RRect.fromRectAndCorners(
      Rect.fromPoints(
          Offset(startX, 0), Offset(startX + size.width * 0.25, size.height)),
      topLeft: borderRadius.topLeft,
      topRight: borderRadius.topRight,
      bottomLeft: borderRadius.bottomLeft,
      bottomRight: borderRadius.bottomRight,
    );

    canvas.drawRRect(rrect, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
